<?php
$post_fields = $_POST['fields'];
$form_fields = get_fields('form', $module_fields);
$form_fields_names = get_names('name', $form_fields);
$list_fields = get_fields('list', $module_fields);
$sort_fields = $list_fields; 
$search_fields = get_fields('search', $module_fields);

$model_name = $controller_name;
$module_name = $module_name;
$module_name_small = $module_name_small;
$input_fields = '';
$update_fields = '';

$all_dropdown = array('status','select');
$dropdown_fields = array();
foreach($module_fields as $each_field) {
	if(in_array($each_field['type'], $all_dropdown))
		$dropdown_fields[] = $each_field;
}

//print_r($dropdown_fields);
//print_r($module_fields);
//die;

$get_date_added = array();
foreach ($module_fields as $each_field) {
	if($each_field['type'] == 'date_added' && ! in_array($each_field['name'], $form_fields_names)) {
		$get_date_added = $each_field;
	}
}

$get_date_modified = array();
foreach ($module_fields as $each_field) {
	if($each_field['type'] == 'date_modified' && ! in_array($each_field['name'], $form_fields_names)) {
		$get_date_modified = $each_field;
	}
}

ob_start();
foreach ($form_fields as $key => $each_field) {
	if ($key == 0)
		echo "\r" . '				" ' . $each_field['name'] . ' = \'" . $this->db->escape($data[\'' . $each_field['name'] . '\']) .';
	else
		echo "\r" . '				"\', ' . $each_field['name'] . ' = \'" . $this->db->escape($data[\'' . $each_field['name'] . '\']) .';
}

if(!empty($get_date_added)) {
	echo "\r" . '				"\', ' . $get_date_added['name'] . ' = \'" . date("Y-m-d H:i:s") .';
}

if(!empty($get_date_modified)) {
	echo "\r" . '				"\', ' . $get_date_modified['name'] . ' = \'" . date("Y-m-d H:i:s") .';
}
$input_fields = ob_get_clean();

ob_start();
foreach ($form_fields as $key => $each_field) {
	if ($key == 0)
		echo "\r" . '				" ' . $each_field['name'] . ' = \'" . $this->db->escape($data[\'' . $each_field['name'] . '\']) .';
	else
		echo "\r" . '				"\', ' . $each_field['name'] . ' = \'" . $this->db->escape($data[\'' . $each_field['name'] . '\']) .';
}

if(!empty($get_date_modified)) {
	echo "\r" . '				"\', ' . $get_date_modified['name'] . ' = \'" . date("Y-m-d H:i:s") .';
}
$update_fields = ob_get_clean();

ob_start();
foreach ($list_fields as $key => $each_field) {
	if ($key == 0)
		echo '`' . $each_field['name'] . '`';
	else
		echo ',`' . $each_field['name'] . '`';
}
$list_fields = ob_get_clean();

ob_start();
foreach ($sort_fields as $each_field) {
	echo "\r" . '			\'' . $each_field['name'] . '\',';
}
$sort_fields = ob_get_clean();

ob_start();
foreach ($dropdown_fields as $each_field) {
	
	$all_select_options = array();	
	$field_options = "";
	preg_match('@`' . $each_field['name'] . '` ([^ \()]+)\(([^ \()]+)\)@', $post_fields, $matches);
	$field_options = $matches['2'];	
	
	if(preg_match("@_id@", $each_field['name'])) $each_field['name'] = str_replace("_id", "", $each_field['name']);
	
	if(strpos($field_options, ',')) {
		$field_options = explode(',', $field_options);
		foreach($field_options as $each_option) {
			
			$each_option = stripslashes($each_option);
			$each_option = substr($each_option, 1, strlen($each_option)-2); 
			
			$all_select_options[] = array(
				'name' => $each_option,
				'value' => $each_option,
			);	
		}
		
	} else {		
		$all_select_options[] = array(
			'name' => 'Option 1',
			'value' => '1',
		);
		
		$all_select_options[] = array(
			'name' => 'Option 2',
			'value' => '2',
		);
		
		$all_select_options[] = array(
			'name' => 'Option 3',
			'value' => '3',
		);
	}
	
	echo "\r" . '	public function get' . str_replace(' ','', $each_field['label']) . 's($data = array()) {' . "\n" .
	'		$' . $each_field['name'] . 's = array();' . "\n" .
	'' . "\n";
	
	
echo '//		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "' . $each_field['name'] . '_table`");
//		
//		if($query->num_rows) {
//			foreach($query->rows as $each_row) {
//				if (isset($data[\'display\']) && !is_null($data[\'display\'])) {
//					$' . $each_field['name'] . 's[$each_row[\'id\']] = $each_row[\'title\'];
//				} else {
//					$' . $each_field['name'] . 's[] = array(
//						\'' . $each_field['name'] . '_id\' => $each_row[\'id\'],
//						\'name\' => $each_row[\'title\'],
//					);	
//				}								
//			}
//		}'. "\n\n";
	
	foreach ($all_select_options as $each_option) {	
		echo '		$' . $each_field['name'] . 's[] = array(' . "\n" .
		'			\'' . $each_field['name'] . '_id\' => \'' . $each_option['value'] . '\',' . "\n" .
		'			\'name\' => \'' . $each_option['name'] . '\',' . "\n" .
		'		);' . "\n\n";
	}
	
	echo '		if (isset($data[\'display\']) && !is_null($data[\'display\'])) {' . "\n" .
			'			$new_' . $each_field['name'] . 's = array();' . "\n" .
			'			foreach($' . $each_field['name'] . 's as $key => $value) {' . "\n" .
			'				$new_' . $each_field['name'] . 's[$value[\'' . $each_field['name'] . '_id\']] = $value[\'name\'];' . "\n" .
			'			}' . "\n" .
			'			$' . $each_field['name'] . 's = $new_' . $each_field['name'] . 's;'. "\n" .
			'		};' . "\n\n";
	
	echo '' . "\n" .
	'		return $' . $each_field['name'] . 's;' . "\n" .
	'	}'. "\n";
}

$new_methods = ob_get_clean();

ob_start();
foreach ($search_fields as $each_field) {
	switch($each_field['type']) {
		case 'status':
		case 'select':
			echo "\r" .'		if (isset($data[\'filter_' . $each_field['name'] . '\']) && !is_null($data[\'filter_' . $each_field['name'] . '\'])) {' . "\n" .
				'			$sql .= " AND ' . $each_field['name'] . ' = \'" . $data[\'filter_' . $each_field['name'] . '\'] . "\'";' . "\n" .
				'		}'. "\n\n";	
		break;
		default:
			echo "\r" .'		if (isset($data[\'filter_' . $each_field['name'] . '\']) && !is_null($data[\'filter_' . $each_field['name'] . '\'])) {' . "\n" .
				'			$sql .= " AND ' . $each_field['name'] . ' LIKE \'%" . $this->db->escape($data[\'filter_' . $each_field['name'] . '\']) . "%\'";' . "\n" .
				'		}'. "\n\n";	
		break;	
	}	
}
$filter_db_fields = ob_get_clean();

$get_status_field = array();
foreach ($module_fields as $each_field) {
	if($each_field['type'] == 'status') {
		$get_status_field = $each_field;
	}
}

$config_safe_delete = 'var $config_safe_delete = 0;';
$list_safe_delete = "";
$update_safe_delete = "";
if($safe_delete && !empty($get_status_field)) {
	$config_safe_delete = 'var $config_safe_delete = 1;';
	$update_safe_delete = '`'.$get_status_field['name'].'` = \'N\'';
	$list_safe_delete = ' AND `'.$get_status_field['name'].'` = \'Y\'';
}


$maches_arr = array (
	'/@config_safe_delete@/',
	'/@filter_db_fields@/',
	'/@input_fields@/',
	'/@list_fields@/',
	'/@list_safe_delete@/',
	'/@model_name@/',
	'/@module_name@/',
	'/@module_name_small@/',
	'/@module_table_name@/',
	'/@module_primary_key@/',
	'/@sort_fields@/',
	'/@update_fields@/',
	'/@update_safe_delete@/',	
	'/@new_methods@/',
	
);

$replace_arr = array (
	$config_safe_delete,
	$filter_db_fields,
	$input_fields,
	$list_fields,
	$list_safe_delete,
	$model_name,
	$module_name,
	$module_name_small,
	$module_table_name,
	$module_primary_key,
	$sort_fields,
	$update_fields,
	$update_safe_delete,	
	$new_methods,	
);

//	preg_match_all('/@([^@]+)@/', $model_file_contents, $maches);
//	sort($maches['1']);
//	print_r(array_unique($maches['1']));

$model_file_contents = preg_replace($maches_arr, $replace_arr, $model_file_contents);
?>